package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.GroupGuigeData;
import com.banzhi.shop.entity.GoodsDetails.Guige;
import com.banzhi.shop.model.CallBack.GroupGuigeCallBack;
import com.banzhi.shop.model.CallBack.GuigeCallBack;
import com.banzhi.shop.model.IGroupGuigeModel;
import com.banzhi.shop.model.IGuigeModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/9.
 */
//任选规格
public class ChoiceGuigeImpl implements IGuigeModel {

    @Override
    public void getGuige(String id, final GuigeCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceGuigeUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("id", id);
       LogUtils.e("GuigeImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Guige guige = gson.fromJson(result, Guige.class);
                        callBack.getGuigeSuccess(guige.getData());
                    } else {
                        callBack.getGuigeError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("GuigeImpl", "---onError----" + ex.toString());
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(791)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

}
